import PubSub from "pubsub-js";
import moment from "moment";
import request from "../../../utils/request";
let appInstance = getApp();
// pages/songDetail/songDetail.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isPlay: false,
    song: {},
    musicId: "",
    musicLink: "",
    currentTime: "00:00",
    durationTime: "00:00",
    currentWidth: 0,
  },
  onLoad: async function (options) {
    console.log('onload...')
    let musicId = options.musicId;
    this.getMusicInfo(musicId);

    // 判断当前页面的音乐是否在播放
    if (
      appInstance.globalData.isMusicPlay &&
      appInstance.globalData.musicId === musicId
    ) {
      this.setData({ isPlay: true });
    }

    this.backgroundAudioManager = wx.getBackgroundAudioManager();
    this.backgroundAudioManager.onPause(() => {
      this.changeState(false);
    });
    this.backgroundAudioManager.onStop(() => {
      this.changeState(false);
    });
    this.backgroundAudioManager.onPlay(() => {
      this.changeState(true);
      appInstance.globalData.musicId = musicId;
    });
    this.backgroundAudioManager.onTimeUpdate(() => {
      let currentTime = moment(
        this.backgroundAudioManager.currentTime * 1000
      ).format("mm:ss");
      let currentWidth =
        (this.backgroundAudioManager.currentTime /
          this.backgroundAudioManager.duration) *
        450;
      this.setData({ currentTime, currentWidth });
    });

    this.backgroundAudioManager.onEnded(() => {
      PubSub.publish("switchType", "next");
    });

    PubSub.subscribe("musicId", async (msg, musicId) => {
      this.getMusicInfo(musicId);
      this.musicControl(true, musicId);
    });
  },
  async getMusicInfo(musicId) {
    let songData = await request("/song/detail", { ids: musicId });
    console.log("总时长：", songData.songs[0].dt);
    let durationTime = moment(songData.songs[0].dt).format("mm:ss");
    this.setData({
      song: songData.songs[0],
      musicId,
      durationTime,
    });
    wx.setNavigationBarTitle({
      title: this.data.song.name,
    });
  },
  changeState(isPlay) {
    this.setData({ isPlay });
    appInstance.globalData.isMusicPlay = isPlay;
  },
  // 封装一个控制音乐播放/暂停的功能函数
  async musicControl(isPlay, musicId, musicLink) {
    if (isPlay) {
      if (!musicLink) {
        let musicLinkData = await request("/song/url", { id: musicId });
        let musicLink = musicLinkData.data[0].url;
        this.setData({
          musicLink,
        });
      }

      this.backgroundAudioManager.src = this.data.musicLink;
      this.backgroundAudioManager.title = this.data.song.name;
    } else {
      this.backgroundAudioManager.pause();
    }
  },
  musicPlay() {
    let isPlay = !this.data.isPlay;
    this.setData({ isPlay });
    let { musicId, musicLink } = this.data;
    this.musicControl(isPlay, musicId, musicLink);
  },
  switchMusic(event) {
    let type = event.currentTarget.id;
    // 停止当前播放的音乐
    this.backgroundAudioManager.stop();
    PubSub.publish("switchType", type);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
